<?php
// 应用公共文件
use app\common\service\redis\PoolRedis;

/**
 * 校验手机号码
 * @param $phone
 * @return bool
 */
function validatePhone($phone)
{
    if (!preg_match("/^1[3456789]\d{9}$/", $phone)) {
        return false;
    }
    return true;
}

/**
 * 获取redis记录的用户id
 * @param $cache_key
 * @param $field
 * @param false $delete
 * @return int|bool
 */
function getRedisCardReturnID($cache_key, $field = '', $delete = false)
{
    $redis = PoolRedis::instance();
    $type = $redis->type($cache_key);
    switch ($type) {
        case \Redis::REDIS_STRING:
            $staff_id = $redis->get($cache_key);
            break;
        case \Redis::REDIS_HASH:
            $staff_id = $redis->hGet($cache_key, $field);
            break;
        case \Redis::REDIS_SET:
        case \Redis::REDIS_LIST:
        case \Redis::REDIS_ZSET:
        default:
            $staff_id = false;
    }
    if ($delete) {
        $redis->del($cache_key);
    }
    return $staff_id;
}

/**
 * 门禁10进制IC卡转乐奇门锁格式
 * @param string $ic_card
 * @return int|mixed|string
 */
function icCardToLeQiFormat($ic_card = '')
{
    if(!is_numeric($ic_card)){
        $ic_card_arr = str_split($ic_card,2);
        krsort($ic_card_arr);
        $ic_card = implode('',$ic_card_arr);
    }
    return $ic_card;
}